using System;
using System.IO;
using System.Configuration.Install;
using System.Reflection;
using System.ServiceProcess;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace WebDavSyncTool
{
    static class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                switch (args[0].ToLower())
                {
                    case "/install":
                        InstallService();
                        break;
                    case "/uninstall":
                        UninstallService();
                        break;
                    case "/console":
                        RunAsConsole();
                        break;
                    default:
                        ShowUsage();
                        break;
                }
            }
            else
            {
                ServiceBase[] ServicesToRun = new ServiceBase[]
                {
                    new WebDavSyncService()
                };
                ServiceBase.Run(ServicesToRun);
            }
        }

        private static void InstallService()
        {
            try
            {
                string installUtilPath = Path.Combine(
                    RuntimeEnvironment.GetRuntimeDirectory(),
                    "InstallUtil.exe");
                
                ProcessStartInfo startInfo = new ProcessStartInfo
                {
                    FileName = installUtilPath,
                    Arguments = Assembly.GetExecutingAssembly().Location,
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = true
                };

                using (Process process = Process.Start(startInfo))
                {
                    process.WaitForExit();
                    if (process.ExitCode == 0)
                    {
                        Console.WriteLine("服务安装成功");
                    }
                    else
                    {
                        Console.WriteLine("服务安装失败");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("服务安装失败: " + ex.Message);
            }
        }

        private static void UninstallService()
        {
            try
            {
                ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                Console.WriteLine("服务卸载成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine("服务卸载失败: " + ex.Message);
            }
        }

        private static void RunAsConsole()
        {
            try
            {
                var service = new WebDavSyncService();
                Console.WriteLine("WebDAV同步服务启动中...");
                service.StartService();
                
                Console.WriteLine("服务已启动。按Q键停止服务...");
                while (Console.ReadKey().Key != ConsoleKey.Q)
                {
                    Console.WriteLine("按Q键停止服务...");
                }
                
                service.StopService();
                Console.WriteLine("服务已停止。");
            }
            catch (Exception ex)
            {
                Console.WriteLine("运行失败: " + ex.Message);
            }
        }

        private static void ShowUsage()
        {
            Console.WriteLine("用法:");
            Console.WriteLine("WebDavSyncTool.exe [/install] [/uninstall] [/console]");
            Console.WriteLine("  /install   - 安装服务");
            Console.WriteLine("  /uninstall - 卸载服务");
            Console.WriteLine("  /console   - 以控制台模式运行");
        }
    }
} 